﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using Menu = Cms.Domain.Menu;

namespace Cms.Web.UI.Admin
{
    public partial class Menus : AdminPage, IMenusView
    {
        private MenusPresenter _presenter;

        #region IMenusView Members

        public void SetMenus(IList<Menu> menus)
        {
            rpMenus.DataSource = menus;
            rpMenus.DataBind();
            trNoHits.Visible = menus.Count == 0;
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Menus.xml"; }
        }


        public void DisplayUnableToDeleteAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new MenusPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('menuPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }


        protected void rpMenus_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var menu = (Menu) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litMenuID = (Literal) e.Item.FindControl("litMenuID");
            var litDescription = (Literal) e.Item.FindControl("litDescription");
            litID.Text = "<span id=\"M" + menu.ID + "\">";
            litName.Text = menu.Name;
            litMenuID.Text = menu.ID.ToString();
            litDescription.Text = menu.Description;
        }


        protected void lbCopy_Click(object sender, EventArgs e)
        {
            int cloneID = _presenter.CopyMenu(int.Parse(hidSelectedMenuID.Value.Substring(1)));
            Response.Redirect("EditMenu.aspx?tab=properties&id=" + cloneID);
            //upnlMenus.Update();
            //iLib.Web.UI.ClientScriptProxy.Current.RegisterStartupScript(pnlActions, this.GetType(), "initMenuList", "initMenuList();", true);
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteMenu(int.Parse(hidSelectedMenuID.Value.Substring(1)));
            upnlMenus.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initMenuList", "initMenuList();",
                                                            true);
        }
    }
}